package com.example.luohao.myrecyclerviewdemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.zhxu.recyclerview.adapter.MultiItemTypeAdapter;
import com.zhxu.recyclerview.base.ItemViewDelegate;
import com.zhxu.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：MyRecyclerViewDemo
 * 类描述：多种条目的的RecyclerView
 * 创建人：luohao
 * 创建时间： 23:28
 * 修改人：luohao
 * 修改时间：23:28
 * 修改备注：
 */

public class Activity2 extends AppCompatActivity {

    private RecyclerView mRV1;
    private ArrayList<Object> userList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_recycler);
        mRV1 = this.findViewById(R.id.mRV1);
        userList = new ArrayList<>();
        userList.add(new UserBean("张三1", "男"));
        userList.add(new UserBean("张三2", "男"));
        userList.add(new UserBean("张三3", "女"));
        userList.add(new UserBean("张三4", "男"));
        userList.add(new UserBean("张三5", "男"));
        userList.add(new UserBean("张三6", "女"));
        userList.add(new UserBean("张三7", "男"));
        userList.add(new UserBean("张三8", "男"));
        userList.add(new UserBean("张三9", "女"));
        userList.add(new UserBean("张三10", "女"));
        userList.add(new UserBean("张三11", "男"));
        userList.add(new UserBean("张三12", "男"));
        userList.add(new UserBean("张三13", "女"));
        userList.add(new UserBean("张三14", "男"));
        userList.add(new UserBean("张三15", "男"));
        userList.add(new UserBean("张三16", "女"));
        userList.add(new UserBean("张三17", "男"));
        userList.add(new UserBean("张三18", "男"));
        userList.add(new UserBean("张三19", "女"));
        mRV1.setLayoutManager(new LinearLayoutManager(this));
        UserAdapter userAdapter = new UserAdapter(this);
        userAdapter.addDataAll(userList);
        mRV1.setAdapter(userAdapter);
    }

    //MultiItemTypeAdapter  多条目Adapter的父类
    public class UserAdapter extends MultiItemTypeAdapter<UserBean> {
        public UserAdapter(Context context, List<UserBean> datas) {
            super(context, datas);

        }

        public UserAdapter(Context context) {
            super(context);
            addItemViewDelegate(new UserItem1());
            addItemViewDelegate(new UserItem2());
        }
    }

    //条目一
    class UserItem1 implements ItemViewDelegate<UserBean> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_common;
        }

        @Override
        public boolean isForViewType(UserBean item, int position) {
            return item.getGender().equals("男");
        }

        @Override
        public void convert(ViewHolder holder, UserBean userBean, int position) {
            holder.setText(R.id.mTV_common, userBean.getName() + "  性别：" + userBean.getGender());
        }
    }

    //条目2
    class UserItem2 implements ItemViewDelegate<UserBean> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_image_text;
        }

        @Override
        public boolean isForViewType(UserBean item, int position) {
            return item.getGender().equals("女");
        }

        @Override
        public void convert(ViewHolder holder, UserBean userBean, int position) {
            holder.setText(R.id.mTV_text, userBean.getName() + "  性别：" + userBean.getGender());
        }
    }
}
